const app = getApp();
const api = require('../../utils/api.js');
const util = require('../../utils/util.js');
Page({
	data: {
		indicatorDots: true,
		autoplay: true,
		interval: 5000,
		duration: 1000,
		latitude: "",
		longitude: "",
		markers: [],
		kc:null,
		commentsList:[],
		commentsAll:0,
		ownerId:"",
		ld:0,
		recommendList:[],
	},
	toBuyKc: function() {
		let kcJson = JSON.stringify(this.data.kc); 
		wx.setStorage({
		  	key:"kcJson",
		  	data:JSON.stringify(this.data.kc),
		  	success:function(){
		  		wx.navigateTo({
					url: '../kcdetail/buyKc'
				})
		  	}
		})
		
	},
	onLoad: function(options) {
		this.setData({
			ownerId: options.id,
		});
		this.findLd();
		this.getDetail(options.id,options.subId);
		this.getCommentsList(options.id);
		this.recommendList(options.subId);
	},
	//查询详情
	getDetail(id,subId) {
		let _this = this;
		api.kcDetail({
			data: {
				id: id,
				subId:subId
			},
			success: function(res) {
				let mark = {
					iconPath: "../../images/mark.png",
					id: 0,
					latitude: res.data.obj.customer.latitude,
					longitude: res.data.obj.customer.longitude,
					width: 50,
					height: 50,
				}
				if(res.data.obj.course.enterStartTime != null){
					res.data.obj.course.enterStartTime = util.formatEndTime(res.data.obj.course.enterStartTime);
				}else{
					if(res.data.obj.courseTime.startTime != null) {
						res.data.obj.course.enterStartTime = util.formatEndTime(res.data.obj.courseTime.startTime);
					}
				}
				res.data.obj.present = res.data.obj.courseTime.coursePoint * _this.data.ld/res.data.obj.course.marketPrice * 100;
				_this.setData({
					kc:res.data.obj,
					markers: [mark],
					presentItems:res.data.obj.course.presentItems.split(",")
				});
				if(res.data.obj.course.courseImgs != ""){
					_this.setData({
						courseImgs:res.data.obj.course.courseImgs.split(",")
					});
				}
			}
		});
	},
	//查询评价
	getCommentsList: function(id) {
		let _this = this;
		api.commentsList({
			data: {
				ownerId: id,
				pageSize: "5",
				pageNum: "1",
			},
			success: function(res) {
				if(res.data.code == 0) {
					res.data.list.rows.forEach(function(item){
						item.createTime = util.formatMinute(new Date(item.createTime));
					});
					_this.setData({
						commentsList: res.data.list.rows,
						commentsAll: res.data.list.total,
					});
				}
			},
		});
	},
	toCommentsList:function(){
		wx.navigateTo({
			url: '../comment/commentList?ownerId=' + this.data.ownerId + "&ownerName=" + this.data.kc.course.courseName
		})
	},
	//查询遛点配置
	findLd() {
		let _this = this;
		api.getMateList({
			data: {
				pageSize: "10",
				pageNum: "1",
				type: "liudian"
			},
			success: function(res) {
				_this.setData({
					ld: res.data.list.rows[0].value
				});
			}
		});
	},
	//课程推荐
	recommendList: function(id) {
		let _this = this;
		api.recommendList({
			data: {
				id: id,
				lng: app.globalData.longitude,
				lat: app.globalData.latitude,
				pageSize: "10",
				pageNum: "1",
			},
			success: function(res) {
				if(res.data.code == 0) {
					res.data.list.rows.forEach(function(item){
						if(item.enterStartTime != null){
							item.itemDate = util.formatDate(new Date(item.enterStartTime));
							item.itemHour = util.formatHour(new Date(item.enterStartTime));
						}else{
							if(item.subTime != null) {
								item.itemDate = util.formatDate(new Date(item.subTime));
								item.itemHour = util.formatHour(new Date(item.subTime));
							}
						}
					});
					_this.setData({
						recommendList: res.data.list.rows,
					});
				}
			},
		});
	},
});